// ezSignTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ezSignTest.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		USES_CONVERSION;
	::CoInitialize(NULL);
	AfxEnableControlContainer();
	CDEzSignCtrl ezCtrl;

	RECT rc;
	rc.top = rc.left = rc.bottom = rc.right = 0;

	AfxMessageBox(_T("1"));
	BOOL bCreate = ezCtrl.Create(_T("EZSIGNCTRL.EzSignCtrlCtrl.1"), _T(""), WS_VISIBLE, rc, this, 0);
	CString msg;
	if(!bCreate)
	{
		::CoUninitialize();
		msg = ezCtrl.GetErrString();
		AfxMessageBox(msg);
		return 10000;
	}

	long ret = 0;
	if(customflag == 4)
	{
		AfxMessageBox(_T("2"));
		ret = ezCtrl.IsSupportPrinterEx(_T(""), strPrinterName, drivername);
		if(ret)
		{
			msg = ezCtrl.GetErrString();
			AfxMessageBox(msg);
			::CoUninitialize();
			return ret;
		}
	}
	else
	{
		if(ret = ezCtrl.IsSupportPrinter(_T(""), drivername))
		{
			::CoUninitialize();
			msg = ezCtrl.GetErrString();
			AfxMessageBox(msg);
			return ret;

		}
	}

	_variant_t copy1;
	_variant_t copy2;

	AfxMessageBox(_T("3"));
	if(ret = ezCtrl.CreateCopyProtectionMark(_T(""), drivername,&copy1, &copy2, NULL))
	{
		::CoUninitialize();
		msg = ezCtrl.GetErrString();
		AfxMessageBox(msg);
		return -10001;
	}

	AfxMessageBox(_T("4"));
	void * pArrayData;

	DWORD pcBufLen = copy1.parray->rgsabound[0].cElements - copy1.parray->rgsabound[0].lLbound;
	SafeArrayAccessData(V_ARRAY(&copy1), &pArrayData);

	uchar *m_pOutData = new uchar[pcBufLen];
	memcpy(m_pOutData, pArrayData, pcBufLen);
	int m_nOutDataLen = pcBufLen;
	SafeArrayUnaccessData(copy1.parray);

	HANDLE hFile = CreateFile(copymark1, GENERIC_WRITE, FILE_SHARE_READ,
		NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		::CoUninitialize();
		msg = ezCtrl.GetErrString();
		AfxMessageBox(msg);
		return -10002;
	}
	DWORD dwWritten = 0;
	WriteFile(hFile, m_pOutData, m_nOutDataLen, &dwWritten, NULL);
	CloseHandle(hFile);

	delete []m_pOutData;

	::CoUninitialize();

	AfxMessageBox(_T("5"));
	}

	return nRetCode;
}
